package edu.uoc.tdp.pac4.client.logandreg;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;


import edu.uoc.tdp.pac4.beans.Course;
import edu.uoc.tdp.pac4.beans.Group;
import edu.uoc.tdp.pac4.beans.Registration;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;

/**
 * Classe auxiliar per crear els RegistrationData
 * @author oscarmateu
 *
 */
public class RegDataHelper {
	
	
	
	public List<RegistrationData> getAllRegistrationData(ClientConnectionHelper connectionHelper_){	
		List<Registration> regList = new ArrayList<Registration>();
		try{
			regList = (List<Registration>) connectionHelper_.getLogAndRegManager().getAllRegByStudent(ClientConnectionHelper.getLoggedUser().getNIF());	
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		} 
		return setRegistrationData(regList, connectionHelper_);	
	}
	
	public List<RegistrationData> getAcceptedRegistrationData(ClientConnectionHelper connectionHelper_){	
		List<Registration> regList = new ArrayList<Registration>();
		try{
			regList = (List<Registration>) connectionHelper_.getLogAndRegManager().getAllAcceptedRegByStudent(ClientConnectionHelper.getLoggedUser().getNIF());	
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		} 
		return setRegistrationData(regList, connectionHelper_);	
	}
	
	public List<RegistrationData> getDeclinedRegistrationData(ClientConnectionHelper connectionHelper_){	
		List<Registration> regList = new ArrayList<Registration>();
		try{
			regList = (List<Registration>) connectionHelper_.getLogAndRegManager().getAllDeclinedRegByStudent(ClientConnectionHelper.getLoggedUser().getNIF());	
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		} 
		return setRegistrationData(regList, connectionHelper_);	
	}
	
	public List<RegistrationData> getPendingRegistrationData(ClientConnectionHelper connectionHelper_){	
		List<Registration> regList = new ArrayList<Registration>();
		try{
			regList = (List<Registration>) connectionHelper_.getLogAndRegManager().getAllPendingRegByStudent(ClientConnectionHelper.getLoggedUser().getNIF());	
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		} 
		return setRegistrationData(regList, connectionHelper_);	
	}
	
	/**
	 * Method to fill the RegistrationData
	 * @param regs
	 * @param connectionHelper_
	 * @return list of resultant ResgitrationData
	 */
	public List<RegistrationData> setRegistrationData(List<Registration> regs, ClientConnectionHelper connectionHelper_){
		
		List<RegistrationData> regDataList = new ArrayList<RegistrationData>();
		
		try{
			
			for (Iterator<Registration> it = regs.iterator() ; it.hasNext(); ) {
				Registration r = it.next();
				Group group = connectionHelper_.getAcademicManager().getGroup(r.getGroupId());
				Course course = connectionHelper_.getMaintenanceManager().findCourseById(r.getCourseId());			
				RegistrationData regData = new RegistrationData(course.getCourseName(),group.getName(),group.getStartDate(),group.getFinalDate(),getStatus(r.getStatus()));
				regDataList.add(regData);
			}
			
			
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		} 

		return regDataList;
	}
	
	
	
	
	public String getStatus(int status){
		switch (status) {
		case 0:
			return LanguageUtils.getMessage("client.regandlog.rbtnlabel.pending");
		case 2:
			return LanguageUtils.getMessage("client.regandlog.rbtnlabel.declined");
		case 1:
			return LanguageUtils.getMessage("client.regandlog.rbtnlabel.accepted");
		}
		return null;
		
	}
	
}
